package com.lin.controller;

import com.lin.domain.common.AjaxResult;
import com.lin.utils.TXFileUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/common")
public class CommonController {

    @Resource
    private TXFileUtil txFileUtil;
    private String apiKey = "sk-6361c7561b644e45a9276956eb81d053";

    /**
     * 文件上传
     */
    @PostMapping("/upload")
    public AjaxResult upload(@RequestParam("file")MultipartFile file) throws IOException {
        String url = txFileUtil.upload(file, "aiFitting");//调用腾讯上传
        return AjaxResult.success(url);
    }

    /**
     * ai试衣生成
     */
    @GetMapping("/aiFitting")
    public AjaxResult aiFitting(String topGarmentUrl, String bottomGarmentUrl, String personImageUrl) {

        // 构建请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("X-DashScope-Async", "enable");
        headers.set("Authorization", "Bearer " + apiKey);

        // 构建请求体
        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("model", "aitryon");
        Map<String, String> input = new HashMap<>();
        input.put("top_garment_url", topGarmentUrl);
        input.put("bottom_garment_url", bottomGarmentUrl);
        input.put("person_image_url", personImageUrl);
        requestBody.put("input", input);
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("resolution", -1);
        parameters.put("restore_face", true);
        requestBody.put("parameters", parameters);

        // 构建请求实体
        HttpEntity<Map<String, Object>> entity = new HttpEntity<>(requestBody, headers);

        // 创建 RestTemplate 实例
        RestTemplate restTemplate = new RestTemplate();

        // 发送请求并获取响应
        String response = restTemplate.postForObject("https://dashscope.aliyuncs.com/api/v1/services/aigc/image2image/image-synthesis/", entity, String.class);

        return AjaxResult.success(response);
    }

    @GetMapping("/getStatus")
    public AjaxResult getStatus(String taskId) {
        // 构建请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("Authorization", "Bearer " + apiKey);

        // 构建请求实体
        HttpEntity<String> entity = new HttpEntity<>(headers);

        // 创建 RestTemplate 实例
        RestTemplate restTemplate = new RestTemplate();

        // 发送请求并获取响应
        ResponseEntity<String> exchange = restTemplate.exchange("https://dashscope.aliyuncs.com/api/v1/tasks/" + taskId, HttpMethod.GET, entity, String.class);
        return AjaxResult.success(exchange);
    }
}
